#include <OsgClassDataSource/Point3DOsg.h>
#include <iostream>
#include <osgDB/ReadFile>
#include <FunctionsPluginOsg.h>
#include <ObjectInspectorDirectorOsg.h>
#include <EDesignerCore.h>
#include <Point3D.h>
#include <Functions.h>
#include <EObjectEvent.h>
#include <Trajectory3D.h>
#include <NodeInfoContainer.h>
Point3DOsg::Point3DOsg( QObject *parent /*= 0*/ ) : MatrixTransformOsg(parent)
{
    m_object = new Point3D();
    this->refreshInfoContainer();

    osg::Node* nodePtr = dynamic_cast<osg::Node*>(m_object.get());
    nodePtr->setNodeMask(0xfffffffe);

    QObject::connect(this, SIGNAL(signalMatrixChanged(QMatrix4x4)), this, SLOT(slotsMatrixChanged(QMatrix4x4)));
}


Point3DOsg::~Point3DOsg()
{

}

double Point3DOsg::getRadius()
{
    double result = -1;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getRadius();
    }
    return result;
}

double Point3DOsg::getFineness()
{
    double result = -1;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getFineness();
    }
    return result;

}

double Point3DOsg::getCoordinateSize()
{
    double result = -1;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getCoordinateSize();
    }
    return result;

}

bool Point3DOsg::getShowShpere()
{
    bool result = true;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getShowShpere();
    }
    return result;

}

bool Point3DOsg::getShowCoordinate()
{
    bool result = true;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getShowCoordinate();
    }
    return result;

}

double Point3DOsg::getCoordinateLineSize()
{
    double result = -1;
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        result = point3DPtr->getCoordinateLineSize();
    }
    return result;
}

void Point3DOsg::setRadius(double radius)
{
    if( EEditorFunctions::doubleEqual(getRadius(), radius) || radius < 0 )
    {
        return;
    }

    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setRadius(radius);
    }

}

void Point3DOsg::setFineness(double fineness)
{
    if( EEditorFunctions::doubleEqual(getFineness(), fineness) || fineness < 0 )
    {
        return;
    }

    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setFineness(fineness);
    }

}

void Point3DOsg::setCoordinateSize(double coorSize)
{
    if( EEditorFunctions::doubleEqual(getCoordinateSize(), coorSize) || coorSize < 0 )
    {
        return;
    }


    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setCoordinateSize(coorSize);
    }

}

void Point3DOsg::setShowShpere(bool boolValue)
{
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setShowShpere(boolValue);
    }

}

void Point3DOsg::setShowCoordinate(bool boolValue)
{
    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setShowCoordinate(boolValue);
    }

}

void Point3DOsg::setCoordinateLineSize(double doubleValue)
{
    if( EEditorFunctions::doubleEqual(getCoordinateLineSize(), doubleValue) || doubleValue < 0 )
    {
        return;
    }


    Point3D* point3DPtr = dynamic_cast<Point3D*>(m_object.get());
    if(point3DPtr != NULL)
    {
        point3DPtr->setCoordinateLineSize(doubleValue);
    }
}

void Point3DOsg::slotsMatrixChanged(QMatrix4x4 matrix)
{
    std::cout<<"Point3DOsg::slotsMatrixChanged"<<std::endl;
    EObjectEvent* pointMatrixChangedEvent = new EObjectEvent((QEvent::Type)(QEvent::Type::User + 100));
    pointMatrixChangedEvent->setEObjectPtr(this);
    pointMatrixChangedEvent->setEventInfo("Point3DOsg::slotsMatrixChanged");

/*
    osg::Node* mNode = dynamic_cast<osg::Node*>(m_object.get());

    osg::Group* parentGroup = mNode->getParent(0);
    NodeInfoContainer* parentInfoContainer = NULL;
    if(parentGroup != NULL)
    {
        parentInfoContainer = dynamic_cast<NodeInfoContainer*>(parentGroup->getUserDataContainer());
        if(parentInfoContainer != NULL)
        {
            EObject* parentEObj = parentInfoContainer->getObjectInspectorItem()->getObjectPtr().data();
            EEditorFunctions::postEvent(parentEObj, pointMatrixChangedEvent);
        }
        else
        {
            parentGroup = mNode->getParent(0)->getParent(0);
            if(parentGroup != NULL)
            {
                parentInfoContainer = dynamic_cast<NodeInfoContainer*>(parentGroup->getUserDataContainer());
                if(parentInfoContainer != NULL)
                {
                    EObject* parentEObj = parentInfoContainer->getObjectInspectorItem()->getObjectPtr().data();
                    EEditorFunctions::postEvent(parentEObj, pointMatrixChangedEvent);
                }
            }

        }
    }
*/
    EObject* parentEObj = getParentNodeOsg();
    if(parentEObj != NULL)
    {
        EEditorFunctions::postEvent(parentEObj, pointMatrixChangedEvent);
    }
    std::cout<<"Point3DOsg::slotsMatrixChanged::postEvent"<<std::endl;

}




